package org.wheel.plugins.toolkit.util.sql.ai;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import org.junit.jupiter.api.Test;
import org.wheel.plugins.toolkit.sql.core.LambdaQueryWrapper;
import org.wheel.plugins.toolkit.sql.core.Wrappers;
import org.wheel.plugins.toolkit.sql.function.F;
import org.wheel.plugins.toolkit.util.sql.entity.WorkPackage;
import org.wheel.plugins.toolkit.util.sql.entity.WorkPackageBO;
import org.wheel.plugins.toolkit.util.sql.entity.WorkPackageMonth;

import java.util.Map;

public class AiTestCase {

    // Hutool-log会按照Slf4j(Logback) > Log4j > Log4j2 > Apache Commons Logging > JDK Logging > Console的顺序自动检测项目中的日志框架
    private static final Log log = LogFactory.get();

    @Test
    void testLambdaFullChain() {
        LambdaQueryWrapper<WorkPackage> qw = Wrappers.lambdaQuery(WorkPackage.class)
                .as("u")
                .select(WorkPackage::getWorkName)
//                .selectCollection(WorkPackageMonth.class, WorkPackageBO::getWorkPackageMonthBOList)   // 自动 t2
//                .select(F.rowNumber()
//                        .over(w -> w.partitionBy(WorkPackage::getGmtCreate)
//                                .orderByDesc(WorkPackage::getGmtModified))
//                        .as(WorkPackageBO::getExecType))
                .rowNumber(over->over.partitionBy(WorkPackage::getWorkName).orderByAsc(WorkPackage::getGmtCreate), WorkPackageBO::getExecType)
                .leftJoin(WorkPackageMonth.class, on -> on.eq(WorkPackageMonth::getWorkPackageId, WorkPackage::getId))
                .eq(WorkPackage::getRevision, 1);

        String sql = qw.getSql();
        Map<String, Object> param = qw.getParamMap();

        log.info("sql: {}", sql);
        log.info("param: {}", param);

//        // Spring 直接执行
//        NamedParameterJdbcTemplate tpl = SpringJdbcAdapter.asTemplate(render);
//        tpl.update(sql, param);
    }
}
